package itheima.reggie.common;

import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.ServletException;
import java.sql.SQLIntegrityConstraintViolationException;


/**
 * 全局异常处理
 */
@ControllerAdvice(annotations = {RestController.class, Controller.class})
@Slf4j
public class GlobalExceptionHandler {
    /**
     * 异常处理方法
     * @return
     */
    @ExceptionHandler()
    public R<String> exceptionHandler( ServletException e){
        log.info(e.getMessage());
        return R.error("失败了");
    }
    @ExceptionHandler(CustomException.class)
    public R<String> exceptionHandel1(CustomException ex){
        log.error(ex.getMessage());
        return R.error(ex.getMessage());
    }
}
